<?php
class Utilities {
	/*
	 * @brief 数组转换
	 * @description 将源数组$arrRaw的中某些字段push到目标数组$arrData中并将key重名为指定的名字。
	 这个方法主要用在ui将其他模板传递的参数转换为模板要求的变量
	 * @param [in]
	 *        $arrKey  =  array  ( $keyRaw1  =>  $keyRes1
	 *                             ............
	 *                           )
	 *        转换定义，其中key为原始数组中的key，value为转换后的key名
	 *        $arrRaw  原数组
	 *        [out]
	 *        $arrData 目标数组
	 */
	public static function arrayKeyConvert ($arrKey,$arrRaw,& $arrData) {
		foreach ($arrKey as $strKey => $strValue) {
			//if (!isset($arrRaw[$strKey])){
			// $arrData[$strValue] = '';
			//      continue;
			//}
			$arrData[$strValue] = $arrRaw[$strKey];
		}
		return true;
	}

	public static function getLogID()
	{
		$arr = gettimeofday();
		return ((($arr['sec']*100000 + $arr['usec']/10) & 0x7FFFFFFF) |
				0x80000000);
	}	
	public static function getClientIP() {   
		if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
			$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
		} 
		elseif (isset($_SERVER["HTTP_CLIENT_IP"])) {
			$ip = $_SERVER["HTTP_CLIENT_IP"];
		} 
		elseif (isset($_SERVER["REMOTE_ADDR"])) {
			$ip = $_SERVER["REMOTE_ADDR"];
		} 
		else {
			$ip = "0.0.0.0";
		}

		$pos = strpos($ip, ',');
		if ($pos > 0) {
			$ip = substr($ip, 0, $pos);
		}
		return trim($ip);
	}

	public static function numToIP($num) {   
		$tmp = (double)$num;
		return sprintf('%u.%u.%u.%u', $tmp & 0xFF, (($tmp >> 8) & 0xFF),
				(($tmp >> 16) & 0xFF), (($tmp >> 24) & 0xFF));
	}

	public static function ipToNum($ip) {
		$n = ip2long($ip);

		/** convert to network order */
		$n =       (($n & 0xFF) << 24)
			| ((($n >> 8) & 0xFF) << 16)
			| ((($n >> 16) & 0xFF) << 8)
			| (($n >> 24) & 0xFF);
		return sprintf('%u', $n);
	}
	public static function stripNoGbk($str){
		$strRaw = trim($str) ;
		$strRet = '';
		$intLen = strlen($strRaw);
		$i = 0;
		/**
		 * 删除 非ascill 码  和非gbk编码 
		 */
		while ($i< $intLen){
			if (ord($strRaw[$i]) ==0xa1 && ord($strRaw[$i+1]) == 0xa1){
				$i++;
			} elseif(ord($strRaw[$i]) >= 0x20 && ord($strRaw[$i]) < 0x7f){ 
				$strRet .= $strRaw[$i];
			}elseif(self::isGBK(ord($strRaw[$i]),ord($strRaw[$i+1])) ){
				$strRet .= $strRaw[$i];
				$strRet .=
					$strRaw[$i+1];
				$i++;
			}
			$i++;
		}
		return trim($strRet);
	}
	static	function isGBK($up,$lower){
		return 
			(($up>=0xb0 && $up<=0xf7 && $lower>=0xa1 && $lower<=0xfe)  ||
			 ($up>=0x81 && $up<=0xa0 && $lower>=0x40 && $lower<=0xfe)  ||
			 ($up>=0xaa && $up<=0xfe && $lower>=0x40 && $lower<=0xa0)  ||
			 ($up>=0xa1 && $up<=0xa9 && $lower>=0xa1 && $lower<=0xfe)  ||
			 ($up>=0xa8 && $up<=0xa9 && $lower>=0x40 && $lower<=0xa0));
	}

	public static function genCoderString ($intMinNum = 10, $intMaxNum = 31) {
		$arr = gettimeofday ();
		srand($arr['sec'] + $arr['usec']);
		$intLen = rand($intMinNum,$intMaxNum);
		$str = md5(md5(rand()));
		$intStart = rand(0,$intMaxNum-$intLen);
		return substr($str,$intStart,$intLen);
	}

	public static function idEncode ($id1,$id2) {
		//var_dump ($id1);
		//var_dump ($id2);
		$strC = fcrypt_id_2hstr(ClubUiInc :: FCRYPT_KEY_CLUB,intval($id1),intval($id2));
		return $strC;
	}
	public static function utf8_to_gbk($str){
#$res_str = iconv("UTF-8", "gbk//IGNORE", $str);
#$res_str =  mb_convert_encoding($str, 'gbk',  'utf-8');
		$res_str = utf8_to_gbk($str, UCONV_INVCHAR_REPLACE) ;
		return $res_str;
	}
	public static function gbk_to_utf8($str){
#$res_str =  mb_convert_encoding($str, 'utf-8',  'gbk');
		$res_str = gbk_to_utf8($str, UCONV_INVCHAR_REPLACE) ;
#iconv("gbk", "UTF-8//IGNORE", $str);
		return $res_str;

	}
	public static function  gbk_to_utf8_array($arr){
		if (is_array($arr)) {
			foreach ($arr as $strKey=>$strTmp){
				if (is_array($strTmp)){
					$arr[$strKey]  = self :: gbk_to_utf8_array($strTmp);
				}
				elseif(is_string($strTmp))
					$arr[$strKey] = self :: gbk_to_utf8($strTmp);
				else $arr[$strKey] = intval($strTmp);
			}
			return ($arr);
		}
		elseif(!is_string($arr))
			return self :: gbk_to_utf8($arr);
		return  ($arr) ;
	}
	public static function  utf8_to_gbk_array($arr){
		if (is_array($arr)) {
			foreach ($arr as $strKey=>$strTmp){
				if (is_array($strTmp))
					$arr[$strKey] =   self :: utf8_to_gbk_array($strTmp);
				else $arr[$strKey] =  self :: utf8_to_gbk($strTmp);
			}
			return $arr;
		}
		else
			return self :: utf8_to_gbk($arr);
	}
	public static function getReferer(){
		$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
		return $referer;
	}
	public static function padString($str, $len, $bolCut, $ch = "\0"){
		if (strlen($str) >= $len) {
			$str = $bolCut ? substr($str, 0, $len-1) . $ch : substr($str, 0,
					$len);
		} else {
			$str = str_pad($str, $len, $ch);
		}
		return $str;
	}
	public static function is_utf8($str)
	{   
		return 1 == preg_match('%^(?:[\x09\x0A\x0D\x20-\x7E]'.  // ASCII
					'| [\xC2-\xDF][\x80-\xBF]'.             //non-overlong 2-byte
					'| \xE0[\xA0-\xBF][\x80-\xBF]'.         //excluding overlongs
					'| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}'.  //straight 3-byte
					'| \xED[\x80-\x9F][\x80-\xBF]'.         //excluding surrogates
					'| \xF0[\x90-\xBF][\x80-\xBF]{2}'.      //planes 1-3
					'| [\xF1-\xF3][\x80-\xBF]{3}'.          //planes 4-15
					'| \xF4[\x80-\x8F][\x80-\xBF]{2}'.      //plane 16
					')*$%xs', $str);
	}
	public static function array_to_str($arrParam, $strDelimit='=', $strJoin=','){
		if (!is_array($arrParam)) return $arrParam;
		$arrItems = array();

		foreach($arrParam as $strKey=>$strVal)
		{
			if (is_array($strVal)){
				$arrItems[] = sprintf("(%s)", self :: array_to_str($strVal, $strDelimit, $strJoin));
			}else 
				$arrItems[] = sprintf("%s%s%s", $strKey, $strDelimit, $strVal);
		}
		return join($strJoin, $arrItems);
	}
}

